package com.yuan.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yuan.cmn.client.DictFeignClient;
import com.yuan.hospital.repository.HospitalRepository;
import com.yuan.hospital.service.HospitalService;
import com.yuan.model.hospital.Hospital;
import com.yuan.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院接口
     *
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {
        //把参数map集合转换对象 Hospital
        String mapString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //判断是否存在数据
        String hosCode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hosCode);
        //如果存在，进行修改
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 根据医院编号查询
     *
     * @param code
     * @return
     */
    @Override
    public Hospital getByHospitalCode(String code) {
        return hospitalRepository.getHospitalByHoscode(code);
    }

    /**
     * 医院列表(条件查询分页)
     *
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospitalPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //hospitalSetQueryVo转换Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //创建对象
        Example<Hospital> example = Example.of(hospital, matcher);
        //调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        //pages.getContent().stream().forEach(item->{
        //    this.setHospitalHosType(item);
        //});
        return pages;
    }

    /**
     * 更新医院上线状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 医院详情信息
     *
     * @param id 医院id
     * @return
     */
    @Override
    public Map<String, Object> getHospitalById(String id) {
        Map<String, Object> map = new HashMap<>();
        //医院基本信息（包含医院等级）
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        map.put("hospital", hospital);
        //预约规则
        map.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    /**
     * 根据医院code获取医院名称
     *
     * @param hospitalCode
     * @return
     */
    @Override
    public String getHospitalName(String hospitalCode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hospitalCode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据医院名称查询
     *
     * @param hospitalName
     * @return
     */
    @Override
    public List<Hospital> findByHospital(String hospitalName) {
        return hospitalRepository.findHospitalByHosnameLike(hospitalName);
    }

    /**
     * 根据医院编号获取医院预约挂号详情
     *
     * @param hospitalCode
     * @return
     */
    @Override
    public Map<String, Object> item(String hospitalCode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHospitalCode(hospitalCode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}
